#include<bits/stdc++.h>
#define i64 long long
#define ull unsigned long long
using namespace std;

const int N = 1e6 + 10, INF = 0x3f3f3f3f;

const i64 MOD = 1e9+7;

i64 Kqow(i64 a, i64 b)
{
    i64 res = 1;
    while (b)
    {
        if (b & 1) res = res * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return res;
}

void solve()
{
    vector<i64> cnt(10);
    i64 m;
    cin >> m;
    i64 mi = 1e9;
    for (int i = 0; i < 10; i++)
    {
        cin >> cnt[i];
        if (i && cnt[i]) mi = min(mi, i * 1LL);
    }

    
    i64 ans = mi;
    if (m == 1 && cnt[0]) cout << 0 << endl;
    else
    {
        i64 D = Kqow(9, MOD - 2);
        cnt[mi]--;
        m--;
        for (int i = 0; i < 10; i++)
        {
            if (cnt[i] >= m)
            {
                ans = ((ans * Kqow(10, m)) % MOD + i * D * (Kqow(10, m) - 1 + MOD) % MOD) % MOD;
                break;
            }
            else {
                ans = ((ans * Kqow(10, cnt[i])) % MOD + i * D * (Kqow(10, cnt[i]) - 1 + MOD) % MOD) % MOD;
            }
            m -= cnt[i];
        }
        cout << ans << endl;
    }
}

signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}